var api_user       = require('API').user,
    api_rank       = require('API').rank,
    roles_for_user = require('API').rbac.roles_for_user,
    rbac_reload    = require('API').rbac.reload;

module.exports = ['$scope', function ($scope) {
    $scope.all_users = [];
    $scope.all_ranks = [];

    (function () {
        api_user.brief(function (json) {
            $scope.$apply(function () {
                $scope.all_users = json;
            });
        });
        api_rank.all(function (json) {
            $scope.$apply(function () {
                $scope.all_ranks = json;
            });
        });
    })();

    function refresh(cb) {
        $scope.cur_user = {};
        $scope.cur_user_unassigned_ranks = [];
        $scope.cur_user_assigned_ranks = [];
        $scope.cur_user_roles = [];
        if (cb)cb();
    }

    refresh();

    //获取用户信息
    $scope.user = function () {
        api_user.brief(function (data) {
            $scope.$apply(function () {
                $scope.user_data = data;
            })
        });
    };
    $scope.user();

    //user ranks
    $scope.user_ranks_show = function (item) {
        var data = $scope.all_users;
        //修改颜色
        if (item.iscolor) {
            item.iscolor = false;
        } else {
            for (var i = 0; i < data.length; i++) {
                $scope.user_data[i].iscolor = false;
            }
            item.iscolor = true;
        }
        $scope.cur_user = item;

        api_rank.ranks_for_user($scope.cur_user, function (cur_ranks) {
            roles_for_user($scope.cur_user.id, function (roles) {
                $scope.$apply(function () {
                    $scope.cur_user_roles = roles;
                    $scope.cur_user_assigned_ranks = cur_ranks;
                    $scope.cur_user_unassigned_ranks =
                        $scope.all_ranks.slice(0).filter(function (r) {
                            return !cur_ranks.some(function (x) {
                                return x.id == r.id
                            })
                        });
                })
            });
        });

    };

    $scope.add_rank = function (rank) {
        var save = $scope.cur_user;
        console.log('rank_to_add', rank);
        api_rank.add_rank_to_user(rank, save, function () {
            $scope.$apply(function () {
                refresh(function () {
                    $scope.user_ranks_show(save);
                });
            });
        });
    };

    $scope.remove_rank = function (rank) {
        var save = $scope.cur_user;
        console.log('rank_to_del', rank);
        api_rank.remove_rank_from_user(rank, save, function () {
            $scope.$apply(function () {
                refresh(function () {
                    $scope.user_ranks_show(save);
                });
            });
        });
    };


    $scope.submit = function () {
        var save = $scope.cur_user;
        rbac_reload(function(){
            $scope.$apply(function () {
                refresh(function () {
                    $scope.user_ranks_show(save);
                });
            });
        })
    }
}];